<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
/**
 * Generic controller for entity class, CI AR.
 * @author hmQuan
 *
 */
class Product extends MY_Controller {
	function __construct(){
		parent::__construct();
		$this->load->model('t_product');
		$this->load->model('t_chat');
	}

	public function create_product(){
		$post = parent::validation_user();

		$log = array();

		$gcm_regid = $post[GCM_REGID];
		$user_id = $post[T_USERS][USER_ID];
		$user_name = $post[T_USERS][USER_NAME];
	
		// validation information product exist
		if(!isset($post[PRODUCT_INFORMATION_CEATERD]) || empty($post[PRODUCT_INFORMATION_CEATERD])){
			$log[PRODUCT_INFORMATION_CEATERD] = 'Missing information product';
		}else{

			$inforProduct = $post[PRODUCT_INFORMATION_CEATERD];
			if(!is_array($inforProduct)){
				$log[PRODUCT_INFORMATION_CEATERD] = 'Value information not is json';
			}
		}

		// validation category
		if(!isset($post[PRODUCT_CATEGORY_ID]) || empty($post[PRODUCT_CATEGORY_ID])){
			$log[PRODUCT_CATEGORY_ID] = 'Missing key or value ' . PRODUCT_CATEGORY_ID;
		}else{

			$category_id = $post[PRODUCT_CATEGORY_ID];
			$this->load->model('t_category');
			$is_validation = $this->t_category->get_data_by_id($category_id);
			// if is_validation == null then category id invalid else select list tag by category id
			if($is_validation == null || empty($is_validation)){
				parent::print_result('Category id does not exits');exit();
			}else{
				$arr_list_tag = $this->t_category->get_list_tag_infor_by_id($category_id);
			}
		}

		// validation product field required 
		// product name
		if(!isset($post[PRODUCT_NAME]) || empty($post[PRODUCT_NAME])){
			$log[PRODUCT_NAME] = 'Miss key or value ' . PRODUCT_NAME;
		}else{
			$productName = $post[PRODUCT_NAME];
		}
		//price
		if(!isset($post[PRODUCT_PRICE])){
			$log[PRODUCT_PRICE] = 'Miss key or value ' . PRODUCT_PRICE;
		}else{
			$productPrice = $post[PRODUCT_PRICE];
		}

		//price
		$productNumber = 1;
		if(isset($post[PRODUCT_NUMBER])){
			$productNumber = $post[PRODUCT_NUMBER];
			if($productNumber <1){
				$productNumber = 1;
			}
		}

		if(isset($post[PRODUCT_DESCRIPTION])){
			$productDescription = $post[PRODUCT_DESCRIPTION];
		}else{
			$productDescription = '';
		}
		if(count($log) > 0){
			parent::print_result($log);exit();
		}else{
			if(count($arr_list_tag) > 0){
				// declare array $arrProductTag to store product information
				$arrProductTag = array();
				// loop $arr_list_tag to get fiels required
				foreach ($arr_list_tag as $key => $categoryField) {
					if($categoryField[CAT_RE_FIELD_IS_REQUIRED] >= 1){
						// confirm id tag
						// var_dump($inforProduct[$categoryField[CAT_RE_FIELD_TAG_ID]]);
						if(isset($inforProduct[$categoryField[CAT_RE_FIELD_TAG_ID]])){
							$productTag = array();
							$productTag[PRO_TAG_TAG_ID] = $categoryField[CAT_RE_FIELD_TAG_ID]; 
							$productTag[PRO_TAG_VALUE] = $inforProduct[$categoryField[CAT_RE_FIELD_TAG_ID]];
							$arrProductTag[] = $productTag;	
						}else{ // id tag required missing
							$log[$categoryField[CAT_RE_FIELD_TAG_ID]] = 'Missing value of tag id';
						}
					}else{
						
						if(isset($inforProduct[$categoryField[CAT_RE_FIELD_TAG_ID]])){
							$productTag = array();
							$productTag[PRO_TAG_TAG_ID] = $categoryField[CAT_RE_FIELD_TAG_ID];
							$productTag[PRO_TAG_VALUE]= $inforProduct[$categoryField[CAT_RE_FIELD_TAG_ID]];
							$arrProductTag[] = $productTag;
						}
					}
				}

				if(count($log) > 0){
					parent::print_result($log);exit();
				}else{
					// create product
					$dataProduct = array();
					$dataProduct[PRODUCT_USER_ID] = $user_id;
					$dataProduct[PRODUCT_CATEGORY_ID] = $category_id;
					$dataProduct[PRODUCT_NAME] = $productName;
					$dataProduct[PRODUCT_PRICE] = $productPrice;
					$dataProduct[PRODUCT_DESCRIPTION] = $productDescription;
					$dataProduct[PRODUCT_NUMBER] = $productNumber;

					if(isset($post[LONGITUDE]) && isset($post[LATITUDE]) ){
						$dataProduct[LONGITUDE] = $post[LONGITUDE];
						$dataProduct[LATITUDE]  = $post[LATITUDE];
					}

					if(isset($post[PRODUCT_ACCEPT_OFFER])){
						$dataProduct[PRODUCT_ACCEPT_OFFER]  = $post[PRODUCT_ACCEPT_OFFER];	
					}

					if(isset($post[PRODUCT_PERCENT_SALE_OFF])){
						$dataProduct[PRODUCT_PERCENT_SALE_OFF]  = $post[PRODUCT_PERCENT_SALE_OFF];	
					}

					if(isset($post[PRODUCT_OFFER_FROM])){
						$dataProduct[PRODUCT_OFFER_FROM]  = $post[PRODUCT_OFFER_FROM];	
					}

					$createProduct = $this->t_product->set_data($dataProduct);
					$id_product = $createProduct;
					// update product tag && update product image path
					if($createProduct != null && $createProduct > 0){
						// $updateImage[PRODUCT_AVATAR] = 'uploads/products/' . $createProduct . '_0.jpg';
						// $updateImage[PRODUCT_IMAGE1] = 'uploads/products/' . $createProduct . '_1.jpg';
						// $updateImage[PRODUCT_IMAGE2] = 'uploads/products/' . $createProduct . '_2.jpg';
						// $updateImage[PRODUCT_IMAGE3] = 'uploads/products/' . $createProduct . '_3.jpg';
						// $this->t_product->update_data_by_id($updateImage, $createProduct);
						// release $dataProduct
						$dataProduct = null;

						// insert new tag
						if(isset($post[TAG_INFO_NEW])){
							// $this->load->model('t_producttag');
							$array_new_tag = $post[TAG_INFO_NEW];
							if(!is_array($array_new_tag) && is_string($array_new_tag)){
								$array_new_tag = json_decode($array_new_tag, true);
							}
							$this->load->model('t_tag');
							$this->load->model('t_producttag');
							foreach ($array_new_tag as $key => $value) {
								$tag_result = $this->t_tag->get_data_by_property(array(TAG_TAGNAME=> $key));
								$count_tag = count($tag_result);
								$id_tag = 0;
								if($count_tag <= 0){
									$id_tag = $this->t_tag->set_data(array(TAG_TAGNAME=> $key , TAG_VALUE_TYPE=> 0));
								}else{
									$id_tag = $tag_result[0][TAG_ID];
								}

								$this->t_producttag->set_data(array(TAG_ID=>$id_tag , PRO_TAG_VALUE=> $value , PRODUCT_ID =>$createProduct));
							}	
						}

						// update product tag
						$this->load->model('t_producttag');
						foreach ($arrProductTag as $key => $productTag) {
							$productTag[PRO_TAG_PRODUCT_ID] = $createProduct;
							$this->t_producttag->set_data($productTag);
						}

						parent::print_result($createProduct, SUCCESS_VALUE);
							
							// push notification to user follows
							require_once('follow_user.php');
							$follow = new Follow_user();
							$arrFollow = $follow->get_user_followed_($user_id);
							if(!empty($arrFollow)){
								foreach ($arrFollow as $key => $follow) {

									$dataChat = array();
					
									$dataChat[CHAT_CONTENT] = $user_name . PRODUCT_CHANGE1;
									$dataChat[CHAT_TYPE] = PUSH_CHANGE_TOPIC_INFOR;
									$dataChat[CHAT_TO] = $follow[FL_USER_FROM_ID];
									$dataChat[CHAT_USER_RECEIVE_MESSAGE_ID] = $id_product;

									$idSave = $this->t_chat->set_data($dataChat);

									parent::push_notification_to_client_via_user_id($follow[FL_USER_FROM_ID], PRODUCT_NEW . $user_name . PRODUCT_NEW_1 , PUSH_CHANGE_TOPIC_INFOR , $id_product);
								}
							}
							exit();

						
						// check again for information product
						// $productValue = $this->t_producttag->get_data_by_property(array(PRO_TAG_PRODUCT_ID => $createProduct));
						// if($productValue != null && count($productValue) == count($arrProductTag)){
						// 	parent::print_result($createProduct, SUCCESS_VALUE);
							
						// 	// push notification to user follows
						// 	require_once('follow_user.php');
						// 	$follow = new Follow_user();
						// 	$arrFollow = $follow->get_user_followed_($user_id);
						// 	if(!empty($arrFollow)){
						// 		foreach ($arrFollow as $key => $follow) {
						// 			parent::push_notification_to_client_via_user_id($follow[FL_USER_FROM_ID], PRODUCT_NEW . $user_name . PRODUCT_NEW_1 , PUSH_CHANGE_TOPIC_INFOR , $id_product);
						// 		}
						// 	}
						// 	exit();
						// }else{
						// 	$this->t_product->unset_data_by_id($createProduct);
						// 	$this->t_producttag->unset_data_by_property(array(PRO_TAG_PRODUCT_ID => $createProduct));
						// 	parent::print_result('Occur error when create product.');exit();	
						// }
					}else{
						parent::print_result('Occur error when create product.');exit();
					}
				}
			}else{
				// list tag null
				parent::print_result('Tag is null');exit();
			}
		}
	}

	public function update_product(){
		$post = parent::validation_user();
		$gcm_regid = $post[GCM_REGID];
		$user_id = $post[T_USERS][USER_ID];
		$user_name = $post[T_USERS][USER_NAME];
		
		if(!isset($post[PRODUCT_ID])){
			parent::print_result('Missing key or value ' . PRODUCT_ID);exit();
		}else{
			$product_id = $post[PRODUCT_ID];
		}

		// validation information product exist
		if(!isset($post[PRODUCT_INFORMATION_CEATERD]) || empty($post[PRODUCT_INFORMATION_CEATERD])){
			parent::print_result('Missing information product' . PRODUCT_INFORMATION_CEATERD);exit();
		}else{

			$arrUpdateInfo = $post[PRODUCT_INFORMATION_CEATERD];

			if(!is_array($arrUpdateInfo)){
				parent::print_result('Value information not is json' . PRODUCT_INFORMATION_CEATERD);exit();
			}
		}

		// get product
		$product = $this->t_product->get_data_by_property(array(PRODUCT_ID => $product_id, PRODUCT_USER_ID => $user_id));

		if(empty($product) || count($product) == 0){
			parent::print_result('Product does not exist');exit();
		}else{
			$dataUpdate = array();
			// product name
			if(isset($post[PRODUCT_NAME])){
				$dataUpdate[PRODUCT_NAME] = $post[PRODUCT_NAME];
			}
			// product price
			if(isset($post[PRODUCT_PRICE])){
				$dataUpdate[PRODUCT_PRICE] = $post[PRODUCT_PRICE];
			}
			// product description
			if(isset($post[PRODUCT_DESCRIPTION])){
				$dataUpdate[PRODUCT_DESCRIPTION] = $post[PRODUCT_DESCRIPTION];
			}

			if(isset($post[PRODUCT_OFFER_ID])){
				$dataUpdate[PRODUCT_OFFER_ID] = $post[PRODUCT_OFFER_ID];
			}

			if(isset($post[PRODUCT_PERCENT_SALE_OFF])){
				$dataUpdate[PRODUCT_PERCENT_SALE_OFF]  = $post[PRODUCT_PERCENT_SALE_OFF];	
			}

			if(isset($post[PRODUCT_OFFER_FROM])){
				$dataUpdate[PRODUCT_OFFER_FROM]  = $post[PRODUCT_OFFER_FROM];	
			}

			if(isset($post[PRODUCT_NUMBER])){
				$dataProduct[PRODUCT_NUMBER] = $post[PRODUCT_NUMBER];	
			}else{
				$dataProduct[PRODUCT_NUMBER] = 1;
			}

			
			// update data in table product
			$this->t_product->update_data_by_property($dataUpdate, array(PRODUCT_ID => $product_id, PRODUCT_USER_ID => $user_id));
			// update data in table product tag
			$this->load->model('t_category');
			$arrCategoryField = $this->t_category->get_list_tag_infor_by_id($product[0][PRODUCT_CATEGORY_ID]);
			$this->load->model('t_producttag');
			foreach ($arrCategoryField as $key => $field) {
				if(isset($arrUpdateInfo[$field[CAT_RE_FIELD_TAG_ID]])){
					//update
					$exist = $this->t_producttag->get_data_by_property(array(PRODUCT_ID => $product_id, PRO_TAG_TAG_ID => $field[CAT_RE_FIELD_TAG_ID]));
					if(empty($exist)){
						$this->t_producttag->set_data(array(PRO_TAG_VALUE => $arrUpdateInfo[$field[CAT_RE_FIELD_TAG_ID]], PRO_TAG_PRODUCT_ID => $product_id, PRO_TAG_TAG_ID => $field[CAT_RE_FIELD_TAG_ID]));
					}else{
						$this->t_producttag->update_data_by_property(array(PRO_TAG_VALUE => $arrUpdateInfo[$field[CAT_RE_FIELD_TAG_ID]]) , array(PRODUCT_ID => $product_id, PRO_TAG_TAG_ID => $field[CAT_RE_FIELD_TAG_ID]));
					}
				}
			}

			$arrProductTag = $this->t_producttag->get_data_by_with_tag_table($product_id);
			$product[0]['information'] = $arrProductTag;


			if(isset($post[TAG_INFO_NEW])){
				// $this->load->model('t_producttag');
				$array_new_tag = $post[TAG_INFO_NEW];
				if(!is_array($array_new_tag) && is_string($array_new_tag)){
					$array_new_tag = json_decode($array_new_tag, true);
				}
				$this->load->model('t_tag');
				$this->load->model('t_producttag');
				foreach ($array_new_tag as $key => $value) {
					$tag_result = $this->t_tag->get_data_by_property(array(TAG_TAGNAME=> $key));
					$count_tag = count($tag_result);
					$id_tag = 0;
					if($count_tag <= 0){
						$id_tag = $this->t_tag->set_data(array(TAG_TAGNAME=> $key , TAG_VALUE_TYPE=> 0));
					}else{
						$id_tag = $tag_result[0][TAG_ID];
					}

					$this->t_producttag->set_data(array(TAG_ID=>$id_tag , PRO_TAG_VALUE=> $value , PRODUCT_ID =>$product_id));
				}	
			}

			// // update product tag
			// $this->load->model('t_producttag');
			// foreach ($arrProductTag as $key => $productTag) {
			// 	$productTag[PRO_TAG_PRODUCT_ID] = $createProduct;
			// 	$this->t_producttag->set_data($productTag);
			// }


			parent::print_result($product[0], SUCCESS_VALUE);
			// push notification to user follows
			require_once('follow_user.php');

			$follow = new Follow_user();
			$arrFollow = $follow->get_user_followed_($user_id);
			if(empty($arrFollow)){
				
				foreach ($arrFollow as $key => $follow) {	
					$dataChat = array();
					$dataChat[CHAT_CONTENT] = 'koko';
					$dataChat[CHAT_TYPE] = PUSH_CHANGE_TOPIC_INFOR;
					$dataChat[CHAT_TO] = 0;
					$dataChat[CHAT_USER_RECEIVE_MESSAGE_ID] = $product_id;

					$idSave = $this->t_chat->set_data($dataChat);

					parent::push_notification_to_client_via_user_id($follow[FL_USER_FROM_ID], $user_name . PRODUCT_CHANGE1 , PUSH_CHANGE_TOPIC_INFOR , $product_id);
				}
			}else{
				
			}
		}
		exit();
	}

	/**
	 * get product by id
	 * @return array|null
	 */
	public function get_product_by_id(){
		$post = $this->input->post();
		$post = parent::check_post_value($post);

		if(!isset($post[PRODUCT_ID]) || empty($post[PRODUCT_ID])){
			parent::print_result(array(T_PRODUCT => 'missing key or value ' . PRODUCT_ID));
			exit();
		}else{
			$product_id = $post[PRODUCT_ID];
		}

		$product = $this->t_product->get_data_by_id($product_id);
		
		if($product == null || count($product) == 0){
			parent::print_result('product id not existed');
		}else{

			$this->load->model('t_producttag');
			$arrProductTag = $this->t_producttag->get_data_by_with_tag_table($product_id);
			// foreach ($arrProductTag as $key => $value) {
			// 	if(empty($value[PRO_TAG_VALUE])){
			// 		unset($arrProductTag[$key]);
			// 	}
			// }
			$product[0]['information'] = $arrProductTag;
			$this->t_product->update_data_by_id(array(PRODUCT_VIEW => $product[0][PRODUCT_VIEW] + 1), $product_id);
			parent::print_result($product[0], SUCCESS_VALUE);
		}
		exit();
	}

	/**
	 * get product by category id
	 * @return array|null
	 */
	public function get_product_by_category_id(){
		$post = $this->input->post();
		$post = parent::check_post_value($post);

		if(!isset($post[PRODUCT_CATEGORY_ID]) || empty($post[PRODUCT_CATEGORY_ID])){
			parent::print_result(array(PRODUCT_CATEGORY_ID => 'missing value'));exit();
		}else{
			$productCategoryId = $post[PRODUCT_CATEGORY_ID];
		}

		$arrProduct = $this->t_product->get_data_by_property(array(PRODUCT_CATEGORY_ID => $productCategoryId));

		if(empty($arrProduct)){
			parent::print_result('No found the products by category id');exit();
		}else{
			$this->load->model('t_producttag');
			foreach ($arrProduct as $key => $product) {
				$arrProductTag = $this->t_producttag->get_data_by_with_tag_table($product[PRODUCT_ID]);
				$arrProduct[$key]['information'] = $arrProductTag;
			}

			parent::print_result($arrProduct, SUCCESS_VALUE);exit();
		}
	}

	public function get_product_by_user_id_total(){
		$post = parent::validation_user();

		 if(!isset($post[PRODUCT_USER_ID]) || empty($post[PRODUCT_USER_ID])){
			$productUserId = $post[T_USERS][USER_ID];
		}else{
			$productUserId = $post[PRODUCT_USER_ID];
		}

		$query = $this->t_product->get_data_by_property(array(PRODUCT_USER_ID => $productUserId), '*', -1);
		if(empty($query)){
			parent::print_result(0,SUCCESS_VALUE);
		}else{
			parent::print_result(count($query),SUCCESS_VALUE);
		}
		exit();
	}


	/**
	 * get list product of user id
	 * @return [type] [description]
	 */
	public function get_product_by_user_id(){
		$post = parent::validation_user();

		if(!isset($post[PRODUCT_USER_ID]) || empty($post[PRODUCT_USER_ID])){
			$productUserId = $post[T_USERS][USER_ID];
		}else{
			$productUserId = $post[PRODUCT_USER_ID];
		}

		if(isset($post[PAGE])){
			$page = $post[PAGE];
		}else{
			$page = -1;
		}
		if(isset($post[PER_PAGE])){
			$per_page = $post[PER_PAGE];
		}else{
			$per_page = -1;
		}

		if($page == -1){
			$arrProduct = $this->t_product->get_data_by_property(array(PRODUCT_USER_ID => $productUserId));	
		}else{
			$arrProduct = $this->t_product->get_data_by_property(array(PRODUCT_USER_ID => $productUserId), '*', $per_page . ',' . $page*$per_page);	
		}
		
		if($arrProduct == null || count($arrProduct) == 0){
			parent::print_result('No found the products by user id');exit();
		}else{

			$this->load->model('t_producttag');
			foreach ($arrProduct as $key => $product) {
				$arrProductTag = $this->t_producttag->get_data_by_with_tag_table($product[PRODUCT_ID]);
				$arrProduct[$key]['information'] = $arrProductTag;
			}

			parent::print_result($arrProduct, SUCCESS_VALUE);exit();
		}
	}

	/**
	 * get product like name
	 * @return [type] [description]
	 */
	public function get_product_by_name(){
		$post = $this->input->post();
		$post = parent::check_post_value($post);
		if(!isset($post[PRODUCT_NAME]) || empty($post[PRODUCT_NAME])){
			parent::print_result('Missing name');exit();
		}

		$arrProduct = $this->t_product->get_data_by_property(array(PRODUCT_NAME .' LIKE ' => '%'.$post[PRODUCT_NAME].'%'));
		if($arrProduct == null || count($arrProduct) == 0){
			parent::print_result('No products found which match your product name');exit();
		}else{
			$this->load->model('t_producttag');
			foreach ($arrProduct as $key => $product) {
				$arrProductTag = $this->t_producttag->get_data_by_with_tag_table($product[PRODUCT_ID]);
				$arrProduct[$key]['information'] = $arrProductTag;
			}
			parent::print_result($arrProduct, SUCCESS_VALUE);exit();
		}
	}


	/**
	 * [get_products_completed get list product is complete : has sold]
	 * @return [type] [description]
	 */
	public function get_products_completed(){
		$post = parent::validation_user();

		$data = array();
		$data[PRODUCT_USER_ID] = $post[T_USERS][USER_ID];

		$arrProductCompleted = $this->t_product->get_product_completed($data);
		if($arrProductCompleted == null || count($arrProductCompleted) == 0){
			parent::print_result("No products");exit();
		}else{
			// $this->load->model('t_producttag');
			// foreach ($arrProductCompleted as $key => $product) {
			// 	$arrProductTag = $this->t_producttag->get_data_by_with_tag_table($product[PRODUCT_ID]);
			// 	$arrProductCompleted[$key]['information'] = $arrProductTag;
			// }
			parent::print_result($arrProductCompleted, SUCCESS_VALUE);exit();
		}
	}

	public function my_product_total(){
		$post = parent::validation_user();

		$user_id = $post[T_USERS][USER_ID];

		$query = $this->t_product->get_data_by_property(array(USER_ID => $user_id), '*', -1);
		if(empty($query)){
			parent::print_result(0, SUCCESS_VALUE);
		}else{
			parent::print_result(count($query), SUCCESS_VALUE);
		}
		exit();
	}

	/**
	 * [apply_offer description]
	 * @return [type] [description]
	 */
	public function apply_offer(){
		$post = parent::validation_user();

		$data[PRODUCT_USER_ID] = $post[T_USERS][USER_ID];

		if(!isset($post[PRODUCT_ID])){
			parent::print_result('Missing key or value null ' . PRODUCT_ID);exit();
		}else{
			$data[PRODUCT_ID] = $post[PRODUCT_ID];
		}

		if(isset($post[PRODUCT_APPLY])){
			$apply = 1;
		}else{
			$apply = 0;
		}

		if($apply == 1){
			if(!isset($post[PRODUCT_OFFER_ID])){
				parent::print_result('Missing key or value null ' . PRODUCT_OFFER_ID);exit();
			}else{
				$offerId = $post[PRODUCT_OFFER_ID];
			}
		}
		
		if($apply == 1){
			$updateOffer = $this->t_product->update_data_by_property(array(PRODUCT_OFFER_ID => $offerId), $data);
		}else{
			$updateOffer = $this->t_product->update_data_by_property(array(PRODUCT_OFFER_ID => 0), $data);
		}

		if(empty($updateOffer)){
			parent::print_result('No affect rows', SUCCESS_VALUE);exit();
		}else{
			parent::print_result('Apply offer success', SUCCESS_VALUE);	
			$this->load->model('t_offer');
			$offer = $this->t_offer->get_data_by_id($offerId);
			if(!empty($offer)){
				parent::push_notification_to_client_via_user_id($offer[0][OFFER_USER_ID], PUSH_MSG_APPLY_OFFER, PUSH_OFFER_ECCEPT);
			}
		}
		exit();
	}

	public function history_sale(){
		$post = parent::validation_user();
		$user_id = $post[T_USERS][USER_ID];

		$history_offer = $this->t_product->get_history_offer($user_id);

		if(empty($history_offer)){
			parent::print_result(0, SUCCESS_VALUE);exit();
		}else{
			parent::print_result($history_offer,SUCCESS_VALUE);exit();
		}
	}

	public function hot_product(){
		$post = $this->input->post();
		$post = parent::check_post_value($post);
		if(isset($post['page'])){
			$page = $post['page'];
		}else{
			$page = 0;
		}

		if(isset($post['per_page'])){
			$per_page = $post['per_page'];
		}else{
			$per_page = 20;
		}
		$not_get_info = 0;
		if(isset($post['not_get_info'])){
			$not_get_info = $post['not_get_info'];
		}

		$arrHotHot = $this->t_product->get_hot_product($page, $per_page);
		if(empty($arrHotHot)){
			parent::print_result('No product');
		}else{
			if($not_get_info == 0){
				$this->load->model('t_producttag');
				foreach ($arrHotHot as $key => $product) {
					$arrProductTag = $this->t_producttag->get_data_by_with_tag_table($product[PRODUCT_ID]);
					$arrHotHot[$key]['information'] = $arrProductTag;
				}	
			}
			
			parent::print_result($arrHotHot, SUCCESS_VALUE);	
		}
		exit();
	}

	public function get_product_by_product_tag(){
		
		$post = $this->input->post();
		$post = parent::check_post_value($post);
		
		$category_id = -1;
		$long = 0;
		$lat = 0;
		$tag_id = 0;
		$last_get = 0;


		if(isset($post['key'])){
			$key = $post['key'];
		}else{
			$error = 'leng so short';
			parent::print_result($error);exit();
		}
		
		if(!isset($post[TAG_ID])){
			parent::print_result('Tag id does not exits');exit();
		}

		if(isset($post[CATEGORY_ID])){
			$category_id = $post[CATEGORY_ID];
		}

		if(isset($post[LONGITUDE]) && isset($post[LATITUDE])){
			$long 	= $post[LONGITUDE];
			$lat 	= $post[LATITUDE];
		}

		if(isset($post[MAX_ID])){
			$last_get = '('.$post[MAX_ID].','.($post[MAX_ID] + 50).')';
		}

		$result_product = $this->t_product->get_product_distance_with_product_tag($long , $lat , ' ' , $tag_id , $category_id , $last_get);

		if(empty($result_product)){
			parent::print_result('No product');
		}else{
			
			parent::print_result($result_product, SUCCESS_VALUE);	
		}
		exit();
	}

	public function get_product_by_tag(){
		
		$post = $this->input->post();
		$post = parent::check_post_value($post);
		
		$category_id = -1;
		$long = 0;
		$lat = 0;
		$tag_id = 0;
		$last_get = 0;

		if(!isset($post[TAG_ID])){
			parent::print_result('Tag id does not exits');exit();
		}

		if(isset($post[CATEGORY_ID])){
			$category_id = $post[CATEGORY_ID];
		}

		if(isset($post[LONGITUDE]) && isset($post[LATITUDE])){
			$long 	= $post[LONGITUDE];
			$lat 	= $post[LATITUDE];
		}

		if(isset($post[MAX_ID])){
			$last_get = '('.$post[MAX_ID].','.($post[MAX_ID] + 50).')';
		}

		$result_product = $this->t_product->get_product_distance_with_tag($long , $lat , '' , $tag_id , $category_id , $last_get);

		if(empty($result_product)){
			parent::print_result('No product');
		}else{
			
			parent::print_result($result_product, SUCCESS_VALUE);	
		}
		exit();
	}

	public function get_product_by_name_with_distance(){
		$post = $this->input->post();
		$post = parent::check_post_value($post);
		
		$key = '';
		$category_id = -1;
		$long = 0;
		$lat = 0;
		$tag_id = 0;
		$last_get = 0;
		$where = '';

		if(!isset($post['key'])){
			parent::print_result('key search null');exit();
		}
		$key = $post['key'];
		//if(!is_string($key) || strlen($key) < 2){
		//	parent::print_result('key search too short');exit();	
		//}
		
		$where = $where.' '.T_PRODUCT.'.'.PRODUCT_NAME.' LIKE \'%'.$key.'%\' ' ;

		if(isset($post[LONGITUDE]) && isset($post[LATITUDE])){
			$long 	= $post[LONGITUDE];
			$lat 	= $post[LATITUDE];
		}

		if(isset($post[MAX_ID])){
			$last_get = $post[MAX_ID].','.($post[MAX_ID] + 50);
		}else{
			$last_get = 0;
		}

		if(isset($post[CATEGORY_ID])){
			$category_id = $post[CATEGORY_ID];
			if($category_id > 0){
				$where = $where.' AND '.CATEGORY_ID.' = '.$category_id;
			}
		}

		$result_product = $this->t_product->get_product_distance($long , $lat , $where  ,$last_get);

		if(empty($result_product)){
			parent::print_result('No product');
		}else{
			
			parent::print_result($result_product, SUCCESS_VALUE);	
		}
		exit();

	}

}